package com.yzg.simple.netty.unpacking.unpacking_example.server;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

/**
 * @author misterWei
 * @create 2019年12月26号:23点02分
 * @mailbox mynameisweiyan@gmail.com
 *
 * 对服务器Handler 进行处理做业务操作
 *
 * TCP 粘包拆包示例代码
 */
public class ServerHandler extends SimpleChannelInboundHandler<ByteBuf>{
    private int limit;





    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        //异常处理一般都是关闭当前管道
        ctx.close();
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf byteBuf) throws Exception {
        System.out.println("服务读了 " + ++ this.limit + "次.");
        byte[] content = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(content);
        System.out.println(new String(content, CharsetUtil.UTF_8));
    }
}
